package com.elcom.roaming.module.synthesis;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;

import com.elcom.roaming.common.config.ConfigModule;
import com.elcom.util.app.AppReport;
import com.elcom.util.app.IApplication;
import com.elcom.util.app.IPCHandlerFactory;
import com.elcom.util.app.IPCShellController;
import com.elcom.util.app.MapManager;
import com.elcom.util.app.QueueManager;
import com.elcom.util.ipc.IPCHandler;
import com.elcom.util.map.BoundConcurrentMap;
import com.elcom.util.queue.BoundBlockingQueue;

public class MonitorApp implements IApplication{
	private String app_name;
	private IPCHandler app_ipc;
	private IPCShellController ipc_shell_ctrl;
	private QueueManager queue_manager;
	private MapManager map_manager;
	private Logger logger;
	
	public MonitorApp() {
		this.app_name = "synthesis";
	}
	
	public void setMonitorLogger(Logger logger){
		this.logger = logger;
	}
	
	public void initMonitor() throws IOException {
		app_ipc = IPCHandlerFactory.createIPCHandler(app_name);
		set_name(app_name);

		queue_manager = new QueueManager();
		map_manager = new MapManager();
	}

	public <T> void addQueue(BlockingQueue<T> queue, String name, int maxSize){
		BoundBlockingQueue<T> boundQueue = new BoundBlockingQueue<T>(queue);
		boundQueue.setName(name);
		boundQueue.setMaxSize(maxSize);
		
		queue_manager.add(boundQueue);
	}
	
	public <K,V> void addMap(ConcurrentHashMap<K, V> map, String name, int maxSize){
		BoundConcurrentMap<K, V> boundMap = new BoundConcurrentMap<K, V>(map);
		boundMap.setMaxSize(maxSize);
		boundMap.setName(name);
		
		map_manager.add(boundMap);
	}
	
	public void runMonitor() {
	    ipc_shell_ctrl = new IPCShellController(logger, app_ipc, this);
	    ipc_shell_ctrl.execute();
	}
	
	@Override
	public int get_index() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String get_info(String[] params) throws Throwable {
		return getReport();
	}

	private String getReport(){
		StringBuilder sbuf = new StringBuilder();
	    
	    AppReport.memory_report(sbuf);
	    queue_manager.queuesReport(sbuf);
	    map_manager.mapsReport(sbuf);
	    
	    return sbuf.toString();
	}

	@Override
	public String get_name() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void reload_config(String[] arg0) throws Throwable {
		ConfigModule.getInstance().reloadConfig();
		
	}

	@Override
	public void restart(String[] arg0) throws Throwable {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void set_index(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void set_name(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void start(String[] arg0) throws Throwable {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stop(String[] arg0) throws Throwable {
		// TODO Auto-generated method stub
		
	}

}
